# coding:utf-8
from flask import render_template, redirect, flash, \
    url_for, request, current_app, jsonify
from flask_login import login_required, current_user

from xshare.model.xInfo import XInfo

from xshare.forms.forms import ChangePasswordForm, EditUserForm, CustomXInfoForm
from xshare import db
from flask import Blueprint

others = Blueprint('others', __name__)


@others.route('/')
@login_required
def index():
    return render_template('admin/index.html')


@others.route('/custom/blog/', methods=['GET', 'POST'])
@login_required
def custom_blog():
    form = CustomXInfoForm()

    navbars = [(1, u'魅力黑'), (2, u'优雅白')]
    form.navbars.choices = navbars

    if form.validate_on_submit():
        blog = XInfo.query.first()
        blog.title = form.title.data
        blog.signature = form.signature.data
        if form.navbars.data == 1:
            blog.navbar = 'inverse'
        if form.navbars.data == 2:
            blog.navbar = 'default'
        db.session.add(blog)
        db.session.commit()

        flash(u'修改博客基本信息成功！', 'success')
        return redirect(url_for('others.custom_blog_info'))

    return render_template('admin/custom_blog_info.html', form=form)


@others.route('/blog/info/')
@login_required
def get_blog_info():
    blog = XInfo.query.first()
    if not blog:
        blog = XInfo()
        blog.title = "nxshare"
        blog.navbar = "default"
        blog.signature = "轻松分享你的内容!"
        db.session.add(blog)
        db.session.commit()
    navbar = 1
    if blog.navbar == 'inverse':
        navbar = 1
    if blog.navbar == 'default':
        navbar = 2
    return jsonify({
        'title': blog.title,
        'signature': blog.signature,
        'navbar': navbar,
    })


@others.route('/account/')
@login_required
def account():
    form = ChangePasswordForm()
    form2 = EditUserForm()

    return render_template('admin/admin_account.html',
                           form=form, form2=form2)


@others.route('/change/password/', methods=['GET', 'POST'])
@login_required
def change_password():
    form = ChangePasswordForm()

    if form.validate_on_submit():
        if current_user.verify_password(form.old_password.data):
            current_user.password = form.password.data
            db.session.add(current_user)
            db.session.commit()
            flash(u'修改密码成功！', 'success')
            return redirect(url_for('others.account'))
        else:
            flash(u'修改密码失败！密码不正确！', 'danger')
            return redirect(url_for('others.account'))


@others.route('/edit/user/', methods=['GET', 'POST'])
@login_required
def edit_user():
    form2 = EditUserForm()

    if form2.validate_on_submit():
        if current_user.verify_password(form2.password.data):
            current_user.username = form2.username.data
            current_user.email = form2.email.data
            db.session.add(current_user)
            db.session.commit()
            flash(u'修改用户信息成功！', 'success')
            return redirect(url_for('others.account'))
        else:
            flash(u'修改用户信息失败！密码不正确！', 'danger')
            return redirect(url_for('others.account'))


@others.route('/help')
@login_required
def help():
    return render_template('admin/help_page.html')
